 
package scm.inventoryControl.managedbean;

import crm.salesOrderProcessing.entity.DeliveryOrderEntity;
import java.io.IOException;
import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import mrp.productionPlanning.managedbean.EditForecastTableManagedBean;
import org.primefaces.event.FlowEvent;
import scm.inventoryControl.entity.PackingOrderEntity;
import scm.inventoryControl.entity.PickingOrderEntity;
import scm.inventoryControl.entity.ShippingOrderEntity;
import scm.inventoryControl.session.stateless.InventoryControlSessionBean;
 
@ManagedBean
@ViewScoped
public class GenerateOrdersManagedBean implements Serializable{

    @EJB
    private InventoryControlSessionBean inventorySessionBean;
    private boolean skip;  
    private static Logger logger = Logger.getLogger(EditForecastTableManagedBean.class.getName()); 
    
    private DeliveryOrderEntity selectedDeliveryOrder;
    private DeliveryOrderEntity selectedForView;
    private PickingOrderEntity pickingOrder;
    private PackingOrderEntity packingOrder;
    private ShippingOrderEntity shippingOrder;
    
    public GenerateOrdersManagedBean() {
        pickingOrder=new PickingOrderEntity();
             packingOrder=new PackingOrderEntity();
             shippingOrder=new ShippingOrderEntity();
             
    }
    
    public List<DeliveryOrderEntity> getDeliveryOrders(){
        return inventorySessionBean.getDeliveryOrders();
    }
      
    public void generateOrders(DeliveryOrderEntity test) throws IOException{
        
       //setSelectedDeliveryOrder((DeliveryOrderEntity)event.getComponent().getAttributes().get("selectedDO"));
       //System.out.println("%%%%%%%%%%%%"+getSelectedDeliveryOrder().getId()); 
       //generate picking Order
        pickingOrder.setGenerateDate(Calendar.getInstance().getTime());
        test.setPickingOrder(pickingOrder);
       // inventorySessionBean.savePickingOrder(pickingOrder);
        //generate packing order
        packingOrder.setGenerateDate(Calendar.getInstance().getTime());
       // packingOrder.setDeliveryOrder(selectedDeliveryOrder);
       // inventorySessionBean.savePackingOrder(packingOrder);
        test.setPackingOrder(packingOrder);
        //generate shipping order
        shippingOrder.setGenerateDate(Calendar.getInstance().getTime());
       // shippingOrder.setDeliveryOrder(selectedDeliveryOrder);
        //inventorySessionBean.saveShippingOrder(shippingOrder);
        test.setShippingOrder(shippingOrder);
        
       //FacesContext.getCurrentInstance().getExternalContext().getFlash().put("salesOrder", selectedSalesOrder);
        selectedDeliveryOrder = test;
        System.out.println("&^^^^^^^^^"+selectedDeliveryOrder.getId());
        FacesContext.getCurrentInstance().getExternalContext().redirect("generateOrdersResult.xhtml");
        
      }
      
    public String onFlowProcess(FlowEvent event) {  
        logger.info("Current wizard step:" + event.getOldStep());  
        logger.info("Next step:" + event.getNewStep());  
          
        if(skip) {  
            skip = false;   //reset in case user goes back  
            return "confirm";  
        }  
        else {  
            return event.getNewStep();  
        }  
    }
    
  
    
    public DeliveryOrderEntity getSelectedDeliveryOrder() {
        return selectedDeliveryOrder;
    }

    public void setSelectedDeliveryOrder(DeliveryOrderEntity selectedDeliveryOrder) {
        this.selectedDeliveryOrder = selectedDeliveryOrder;
    }

    public DeliveryOrderEntity getSelectedForView() {
        return selectedForView;
    }

    public void setSelectedForView(DeliveryOrderEntity selectedForView) {
        this.selectedForView = selectedForView;
    }

    public PickingOrderEntity getPickingOrder() {
        return pickingOrder;
    }

    public void setPickingOrder(PickingOrderEntity pickingOrder) {
        this.pickingOrder = pickingOrder;
    }

    public PackingOrderEntity getPackingOrder() {
        return packingOrder;
    }

    public void setPackingOrder(PackingOrderEntity packingOrder) {
        this.packingOrder = packingOrder;
    }

    public ShippingOrderEntity getShippingOrder() {
        return shippingOrder;
    }

    public void setShippingOrder(ShippingOrderEntity shippingOrder) {
        this.shippingOrder = shippingOrder;
    }
}
